﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.Permission.Data
{
    /// <summary>
    /// 数据访问类ModuleData。
    /// </summary>
    public class ModuleData
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "ModuleId", "ModuleName", "ModuleAlias", "ModuleFlag", "ModuleDes" };
        private string tableName = "HF_Module";
        private string keyField = "ModuleId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(ModuleEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@ModuleId", entity.ModuleId, typeof(string));
            sqlDataItem.AppendParameter("@ModuleName", entity.ModuleName, typeof(string));
            sqlDataItem.AppendParameter("@ModuleAlias", entity.ModuleAlias, typeof(string));
            sqlDataItem.AppendParameter("@ModuleFlag", entity.ModuleFlag, typeof(bool));
            sqlDataItem.AppendParameter("@ModuleDes", entity.ModuleDes, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public ModuleEntity GetModuleEntity(DataRow dr)
        {
            ModuleEntity entity = new ModuleEntity();
            string value = "";
            entity.ModuleId = dr["ModuleId"].ToString();
            entity.ModuleName = dr["ModuleName"].ToString();
            entity.ModuleAlias = dr["ModuleAlias"].ToString();
            value = dr["ModuleFlag"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                if ((value == "1") || (value.ToLower() == "true"))
                {
                    entity.ModuleFlag = true;
                }
                else
                {
                    entity.ModuleFlag = false;
                }
            }
            entity.ModuleDes = dr["ModuleDes"].ToString();
            return entity;
        }

        #endregion

        #region  构造函数
        public ModuleData()
        { }
        public static ModuleData GetInstance()
        {
            return new ModuleData();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(ModuleEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(ModuleEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string moduleId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, moduleId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="moduleId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string moduleId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, moduleId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="ModuleId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetModuleTable(string moduleId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, moduleId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="moduleId">主键Id</param>
        /// <returns>DataTable</returns>
        public ModuleEntity GetModuleEntity(string moduleId)
        {
            DataTable dt = GetModuleTable(moduleId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetModuleEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法
        /// <summary>
        /// 获取所有模块
        /// </summary>
        /// <returns>DataTable</returns>
        public  DataTable GetAllModules()
        {
            try
            {
                string tmpSql = "select * from HF_Module order by ModuleId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获取所有模块
        /// </summary>
        /// <returns>Entitys</returns>
        public List<ModuleEntity> GetAllModuleEntitys()
        {
            try
            {
                List<ModuleEntity> result = new List<ModuleEntity>();
                DataTable dt = GetAllModules();
                foreach (DataRow dr in dt.Rows)
                {
                    ModuleEntity entity = GetModuleEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得组能访问的模块
        /// </summary>
        /// <param name="groupId">权限组Id</param>
        /// <returns>模块表</returns>
        public  DataTable GetPowerModules(string groupId)
        {
            string tmpSql = "select distinct m.ModuleId,m.ModuleName,m.ModuleDes,m.ModuleAlias,ModuleFlag from HF_Purview p " +
                           " LEFT  JOIN "+
                           " HF_Module m ON LEFT(p.OperationId, 2) = m.ModuleId where GroupId=@GroupId";
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.AppendParameter("@GroupId", groupId);
            sqlItem.CommandText = tmpSql;
            ClientDBAgent agent = new ClientDBAgent();
            return agent.ExecuteDataTable(sqlItem);

        }
        /// <summary>
        /// 获取系统最大的模块Id
        /// </summary>
        /// <returns>2位模块编码</returns>
        public string GetMaxModuleId()
        {
            string tmpSql = "select moduleid from hf_module order by moduleid desc ";        
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            ClientDBAgent agent = new ClientDBAgent();
            string MaxId = agent.ExecuteScalar(sqlItem);
            if (string.IsNullOrEmpty(MaxId))
            {
                MaxId = "00";
            }
            return MaxId;
        }
        /// <summary>
        /// 获得组能访问的模块
        /// </summary>
        /// <param name="groupId">权限组Id</param>
        /// <returns>Entitys</returns>
        public List<ModuleEntity> GetPowerModuleEntitys(string groupId)
        {
            try
            {
                List<ModuleEntity> result = new List<ModuleEntity>();
                DataTable dt = GetPowerModules(groupId);
                foreach (DataRow dr in dt.Rows)
                {
                    ModuleEntity entity = GetModuleEntity(dr);
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 模块下是否包含功能
        /// </summary>
        /// <param name="moduleId">模块Id</param>
        /// <returns>是否</returns>
        public bool HasFunction(string moduleId)
        {
            try
            {
                string tmpSql = "select * from HF_Function where  " +
                               "  left(FunctionId, 2) = @moduleId ";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.AppendParameter("@moduleId", moduleId);
                sqlItem.CommandText = tmpSql;
                ClientDBAgent agent = new ClientDBAgent();
                return agent.RecordExists(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

